Namespace OpenArchive
    Namespace Collections
        Public Class ComponentList
            Inherits Dictionary(Of String, Document.Components.IComponent)
            Private m_Document As Document.Document
            Friend Event ReadComponentRequested As ReadComponentRequestedEventHandler
            Delegate Sub ReadComponentRequestedEventHandler(ByVal sender As Object, ByVal eventargs As System.Events.ReadEventArguments)
            Public Sub New(ByVal Document As Document.Document)
                m_Document = Document
            End Sub
            Public Shadows Sub Add(ByVal comp As Document.Components.IComponent)
                If Not m_Document.ArchiveProperties.Archive Is Nothing Then
                    AddHandler comp.ReadRequested, AddressOf Read
                End If
                Dim i As Integer
                For i = 0 To 99
                    If MyBase.ContainsKey(i.ToString("D2") & "." & comp.GetType.Name) Then
                        i = i + 1
                    Else
                        If i > 0 Then
                            If comp.IsSingleInstanceType Then
                                Throw New Exception("This component is SingleInstanceType")
                            End If
                        End If
                        MyBase.Add(i.ToString("D2") & "." & comp.GetType.Name, comp)
                        Exit For
                    End If
                Next
            End Sub
            Friend Shadows Sub Add(ByVal Instance As Integer, ByRef comp As Document.Components.IComponent)
                If Not m_Document.ArchiveProperties.Archive Is Nothing Then
                    AddHandler comp.ReadRequested, AddressOf Read
                End If
                MyBase.Add(Instance.ToString("D2") & "." & comp.GetType.Name, comp)
            End Sub
            Private Sub Read(ByVal sender As Document.Components.IComponent)
                RaiseEvent ReadComponentRequested(Me, New System.Events.ReadEventArguments(m_Document, sender))
                RemoveHandler sender.ReadRequested, AddressOf Read
            End Sub
        End Class


    End Namespace
End Namespace
